#!/bin/sh
# VoiceOne tools to allow operations on file and system level
# This file must be set sudo root or set uid and have owner root
# Usage: vo-tools.sh <cmd> [<param1>]  <param2>] etc.
# 

# absolute path to configuration file for shell scripts used by voiceone_webservices
# usually the same dir as the script directory /var/www/html/voiceone_webservices/config/script/
VOCFGDIR="/var/www/html/voiceone_webservices/config/script/"

########################################
# Init and check if config file exists
########################################
test -f "$VOCFGDIR/vo.cfg" && . "$VOCFGDIR/vo.cfg"
if [ "$CFG" != "ok" ]; then echo "ERROR: Could not find or initialize configuration file:vo.cfg"; exit 1; fi

########################################
# Calculate global used vars
########################################
# Locate and store type of Distro used
if [ -d /etc/sysconfig ]; then
  DISTRO=REDHAT
elif [ -f /etc/debian_version ]; then
  DISTRO=DEBIAN
else
  DISTRO=UNKNOWN
fi

# Locate and store AST_SPOOL_DIR
AST_SPOOL_DIR=`grep astspooldir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_SPOOL_DIR="`expr "$AST_SPOOL_DIR" : '[ ]*\(.*\)[ ]*$'`" 

# Locate and store AST_SOUND_DIR
AST_SOUND_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_SOUND_DIR="`expr "$AST_SOUND_DIR" : '[ ]*\(.*\)[ ]*$'`"
AST_SOUND_DIR="$AST_SOUND_DIR/sounds" 

# Locate and store AST_AGI_DIR
AST_AGI_DIR=`grep astagidir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_AGI_DIR="`expr "$AST_AGI_DIR" : '[ ]*\(.*\)[ ]*$'`" 

# Locate and store AST_KEY_DIR
AST_KEY_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_KEY_DIR="`expr "$AST_KEY_DIR" : '[ ]*\(.*\)[ ]*$'`" 
AST_KEY_DIR="$AST_KEY_DIR/keys"

# Locate and store AST_MOH_DIR
AST_MOH_DIR=`grep astvarlibdir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_MOH_DIR="`expr "$AST_MOH_DIR" : '[ ]*\(.*\)[ ]*$'`"
AST_MOH_DIR="$AST_MOH_DIR/mohmp3"

# Locate and store AST_RUN_DIR
AST_RUN_DIR=`grep astrundir $AST_CONF_DIR/asterisk.conf | cut -d '>' -f 2`
AST_RUN_DIR="`expr "$AST_RUN_DIR" : '[ ]*\(.*\)[ ]*$'`"

# Location of zaptel kernel module configuration
if [ $DISTRO = DEBIAN ]; then
  ZAPTEL_KMOD=/etc/default/zaptel
elif [ $DISTRO = REDHAT ]; then
  ZAPTEL_KMOD=/etc/sysconfig/zaptel
fi

########################################
# Check some important global used vars
########################################
# Check if Distro used on this system is supported
if [ "$DISTRO" = "UNKOWN" ]; then echo "ERROR: Could not locate type of Linux Distribution"; exit 1; fi
# Check access to asterisk configuration 
if [ ! -f "$AST_CONF_DIR/asterisk.conf" ]; then echo "ERROR: Could not locate Asterisk configuration"; exit 1; fi

#
### Functions Part ###
#

# return AST_SOUND_DIR
getSoundsDir () {
  echo "$AST_SOUND_DIR";
}

# return AST_MOH_DIR
getMohDir () {
  echo "$AST_MOH_DIR";
}

# make a file.txt that list all the files in aterisck/sounds
soundsInit () {
  if [ -e $1/sounds/filesList.txt ]; then
    rm -rf $1/sounds/filesList.txt
  fi
  if [ ! -d "$1/sounds/" ]; then
    mkdir $1/sounds
  fi
  if [ -d "$AST_SOUND_DIR" ]; then
    ls -lR $AST_SOUND_DIR > $1/sounds/filesList.txt
  fi
}
# overwrite an audio file
soundsCopy () {
  if [ -e $1 ]; then
    if [ -e $2 ]; then
      cp -f $1 $2
    fi
  fi
}

# copy audio file from sounds folder to sandbox
soundsMoveToSandbox () {
  if [ ! -d $2 ]; then
    mkdir $2
  fi
  if [ -e $1 ]; then
    sox $1 -r 8000 -c 1 -s -w $3
  fi
}

# move audio file from sandbox to sounds folder
soundsMoveToSounds () {
  if [ ! -d "$AST_SOUND_DIR/voiceone/tmp" ]; then
    mkdir $AST_SOUND_DIR/voiceone/tmp
  fi
  if [ -e $1 ]; then
    sox $1 -r 8000 -c1 $AST_SOUND_DIR/voiceone/tmp/$2 resample -ql 
  fi
}

# remove all audio files from sandbox/sounds
soundsClear () {
  rm -f $1
}

# make an empty file in /voiceone/custom+
soundsCreateEmptyFile () {
  if [ ! -d "$1" ]; then
    mkdir $1
  fi
  if [ ! -e "$1/$2.gsm" ]; then
      touch $1/$2.gsm
  fi
}

# make a songs list in webservices's sandbox
mohMakeSongsList () {
  if [ -e $2/moh/songsList.txt ]; then
    rm -rf $2/moh/songsList.txt
  fi
  if [ -d "$AST_MOH_DIR/$1" ]; then
    if [ ! -d "$2/moh/" ]; then
         mkdir $2/moh
    fi
    ls -l $AST_MOH_DIR/$1 >> $2/moh/songsList.txt
   fi  
}

# create a symbolic link in class folder
mohSetSong () {
  if [ ! -d "$AST_MOH_DIR/voiceone/$1" ]; then
    mkdir "$AST_MOH_DIR/voiceone/$1"
  fi
  if [ -e "$AST_MOH_DIR/voiceone/$2" ]; then
    ln -s "$AST_MOH_DIR/voiceone/$2" "$AST_MOH_DIR/voiceone/$1/$3"
  fi
}    

# delete a class folder
mohDelFolder () {
  if [ -d "$AST_MOH_DIR/voiceone/$1" ]; then
    rm -rf "$AST_MOH_DIR/voiceone/$1"
  fi
} 

# move an audio file from sandbox to moh/voiceone folder
mohMoveSong () {
  if [ -d "$AST_MOH_DIR/voiceone" ]; then
    cp "$1" "$AST_MOH_DIR/voiceone/$2"
  fi
} 

# delete an audio file and its symbolic links
mohDelSong () {
  if [ -e "$AST_MOH_DIR/voiceone/$1" ]; then
    rm -f "$AST_MOH_DIR/voiceone/$1"
    if [ $(find /var/lib/asterisk/mohmp3/voiceone/ -lname "$1" -o -lname "*/$1" | wc -l) -ne "0" ]; then
      rm `find /var/lib/asterisk/mohmp3/voiceone/ -lname "$1" -o -lname "*/$1"`
    fi
  fi
}
 
# copy voicemail description in webservices's sandbox
voicemail () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ ! -d "$2/voicemail/" ]; then
          mkdir $2/voicemail
      fi
      if [ ! -d "$2/voicemail/$1" ]; then
          mkdir $2/voicemail/$1
      fi
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/INBOX" ]; then
        rm -rf $2/voicemail/$1/*
        echo INBOX >> $2/voicemail/$1/$1_list.txt
        dir $AST_SPOOL_DIR/voicemail/default/$1/INBOX/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
        chmod 666 $2/voicemail/$1/$1_list.txt
        cat $AST_SPOOL_DIR/voicemail/default/$1/INBOX/*.txt >> $2/voicemail/$1/$1_msg.txt
        chmod 666 $2/voicemail/$1/$1_msg.txt
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Old" ]; then
          echo Old >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Old/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Old/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Family" ]; then
          echo Family >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Family/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Family/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Friends" ]; then
          echo Friends >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Friends/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Friends/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi 
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/Work" ]; then
          echo Work >> $2/voicemail/$1/$1_list.txt
          dir $AST_SPOOL_DIR/voicemail/default/$1/Work/ -1 | grep .wav >> $2/voicemail/$1/$1_list.txt
          chmod 666 $2/voicemail/$1/$1_list.txt
          cat $AST_SPOOL_DIR/voicemail/default/$1/Work/*.txt >> $2/voicemail/$1/$1_msg.txt
          chmod 666 $2/voicemail/$1/$1_msg.txt
        fi 
      fi
    fi
}

# Transfer voicemail message from asterisk's folder to wb's folder
transferVoiceMailMessage (){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$2/voicemail/$1" ]; then
        if [ ! -d "$2/voicemail/$1/$4" ]; then
          mkdir $2/voicemail/$1/$4       
        fi
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then     
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$4/$3" ]; then  
            cp $AST_SPOOL_DIR/voicemail/default/$1/$4/$3 $2/voicemail/$1/$4
            chmod 777 $2/voicemail/$1/$4/$3
            exit 0  
          fi
        else
          exit 1   
        fi     
      else
        exit 1   
      fi
    fi
}

# Move voicemail message in another voicemail's folder
messageChangeFolder () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
        if [ ! -d "$AST_SPOOL_DIR/voicemail/default/$1/$3" ]; then
        mkdir $AST_SPOOL_DIR/voicemail/default/$1/$3
      fi
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/$5" ]; then
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$5/$4" ]; then
          mv $AST_SPOOL_DIR/voicemail/default/$1/$5/$4 $AST_SPOOL_DIR/voicemail/default/$1/$3/$2
        fi
      fi  
    fi
}

# Delete a voicemail message
deleteVoicemailMessage () {
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      rm -f $AST_SPOOL_DIR/voicemail/default/$1/$2.*
    fi
}

# Transfer voicemail message from sounds folder to voicemail folder
transferVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then   
        if [ ! -d "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER" ]; then 
          mkdir $AST_SPOOL_DIR/voicemail/default/$1/ANSWER
        fi
        if [ -e "$AST_SOUND_DIR/voiceone/voicemail/$2.gsm" ]; then
          mv -f $AST_SOUND_DIR/voiceone/voicemail/$2.gsm $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm
          sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s -w $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav
          sox $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm -r 8000 -c 1 -s -w $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV
        fi
      fi
    fi
}

# Put in the ws's sendbox the names of the files in ANSWER folder 
getVoiceMailAnswers(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$2/voicemail/$1" ]; then
        if [ -d "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER" ]; then
          rm -rf $2/voicemail/$1/$1_answers.txt
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/unavail.gsm" ]; 
            then 
              echo "*unavailable" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/unavail.gsm` >> $2/voicemail/$1/$1_answers.txt;  
            else 
              echo "*unavailable none" >> $2/voicemail/$1/$1_answers.txt;
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/busy.gsm" ]; 
            then 
              echo "*busy" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/busy.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*busy none" >> $2/voicemail/$1/$1_answers.txt;
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/temp.gsm" ]; 
            then 
              echo "*temp" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/temp.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*temp none" >> $2/voicemail/$1/$1_answers.txt;  
          fi
          if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/greet.gsm" ]; 
            then 
              echo "*greet" `md5sum $AST_SPOOL_DIR/voicemail/default/$1/greet.gsm` >> $2/voicemail/$1/$1_answers.txt;
            else
              echo "*greet none" >> $2/voicemail/$1/$1_answers.txt;  
          fi
          md5sum $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/*.gsm >> $2/voicemail/$1/$1_answers.txt
          chmod 666 $2/voicemail/$1/$1_answers.txt  
        fi
      fi
    fi
}

# Transfer and rename voicemail message from ANSWER folder to mailbox folder
setVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -d "$AST_SPOOL_DIR/voicemail/default/$1" ]; then   
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.gsm $AST_SPOOL_DIR/voicemail/default/$1/$3.gsm
        fi
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.wav $AST_SPOOL_DIR/voicemail/default/$1/$3.wav
        fi
        if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV" ]; then 
          chmod 755 $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV 
          cp -f $AST_SPOOL_DIR/voicemail/default/$1/ANSWER/$2.WAV $AST_SPOOL_DIR/voicemail/default/$1/$3.WAV
        fi
      fi
    fi
}

# remove from "voicemail/default/<exten>" folder, "<filename>.*" files 
deleteVoiceMailAnswer(){
    if [ -e "$AST_CONF_DIR/asterisk.conf" ]; then
      if [ -e "$AST_SPOOL_DIR/voicemail/default/$1/$2.gsm" ]; then
        rm -rf $AST_SPOOL_DIR/voicemail/default/$1/$2.*
      fi
    fi 
}

# delete an audiofile in asterisk sound dir
deleteAudiofile () {
    if [ -f "$AST_SOUND_DIR/$1" ]; then
      rm  -f "$AST_SOUND_DIR/$1"
      exit 0
    else
	  echo "deleteAudio: File:$AST_SOUND_DIR/$1 does not exists."
	  exit 1
	fi
}

# rename an audiofile in asterisk sound dir
renameAudiofile () {
    if [ -f "$AST_SOUND_DIR/$1" ]; then
      mv  -f "$AST_SOUND_DIR/$1" "$AST_SOUND_DIR/$2"
      exit 0
    else
	  echo "RenameAudio: Old file:$AST_SOUND_DIR/$1 does not exists."
	  exit 1
	fi
}

# execute the setup procedure
install() {
  mkdir $AST_SOUND_DIR/voiceone
  mkdir $AST_SOUND_DIR/voiceone/custom
  mkdir $AST_SOUND_DIR/voiceone/IVR
  mkdir $AST_SOUND_DIR/voiceone/voicemail
  mkdir $AST_SOUND_DIR/voiceone/tmp
  mkdir $AST_MOH_DIR/voiceone
  mkdir $AST_MOH_DIR/voiceone/default
    
  chmod a+w $1/$VOICEONE_RELDIR/admin/public/template/cache/
  chmod a+w $1/$VOICEONE_RELDIR/admin/public/script/*
  chmod a+w $1/$VOICEONE_RELDIR/sandbox/
  
  chmod a+x $1/$VOICEONE_WEBSERVICES_RELDIR/config/script/*
  chmod a+w $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/
  
  # CALCULATE THE PHP SHABANG
  if RESULT=`which "php-cgi" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  elif RESULT=`which "php4-cgi" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  elif RESULT=`which "php" 2>/dev/null`; then
    SHABANG="#!$RESULT -q"
  else
    echo "ERROR: No php interpreter found"
    exit 1
  fi
  
  # WRITE THE AGI FILES
  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/dial.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php

  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/incoming.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php

  echo $SHABANG > $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php
  echo "<? include (\"$1/$VOICEONE_WEBSERVICES_RELDIR/agi/record.inc.php\"); ?> " >> $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php

  # MOVE THE PHP FILES TO THE AGI DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/dial.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/incoming.php $AST_AGI_DIR
  mv $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/record.php $AST_AGI_DIR

  cp $1/$VOICEONE_WEBSERVICES_RELDIR/sandbox/agi-callback.agi $AST_AGI_DIR
  
  if [ $(ls $AST_MOH_DIR/*.* | wc -l) -gt 0 ] ; then 
    # LINK DEFAULT MOH FILES FROM ASTRISK FOLDER TO VOICEONE FOLDER
    ln -s $AST_MOH_DIR/*.* $AST_MOH_DIR/voiceone/
    # LINK DEFAULT MOH FILES FROM VOICEONE FOLDER TO DEFAULT FOLDER
    ln -s $AST_MOH_DIR/voiceone/*.* $AST_MOH_DIR/voiceone/default
  fi
  
  # ASSIGN THE SAME OWNER OF THE AGI DIR TO THE FILES INSIDE IT
  UGIDS=`stat -c %U:%G $AST_AGI_DIR`
  
  chown ${UGIDS} $AST_AGI_DIR/dial.php
  chown ${UGIDS} $AST_AGI_DIR/incoming.php
  chown ${UGIDS} $AST_AGI_DIR/agi-callback.agi
  chown ${UGIDS} $AST_AGI_DIR/record.php

  # ASSIGN EXECUTION PERMISSION TO THE PHP AGI FILES
  chmod a+x $AST_AGI_DIR/dial.php
  chmod a+x $AST_AGI_DIR/incoming.php
  chmod a+x $AST_AGI_DIR/agi-callback.agi
  chmod a+x $AST_AGI_DIR/record.php

  # ASSIGN THE SAME OWNER OF THE SOUNDS DIR TO THE FILES INSIDE IT
  chown -R ${UGIDS} $AST_SOUND_DIR/voiceone
  
  # ASSIGN THE SAME OWNER OF THE MOH DIR TO THE FILES INSIDE IT
  chown -R ${UGIDS} $AST_MOH_DIR/voiceone

}

inKey() {
  chmod 644 $1.*
  if [ -e $1.key ]; 
  then
    mv $1.key $AST_KEY_DIR
    cp $1.pub $AST_KEY_DIR
  else
    mv $1.pub $AST_KEY_DIR
  fi

  UGIDS=`stat -c %U:%G $AST_KEY_DIR`
  chown ${UGIDS} $AST_KEY_DIR/*
}

misdnConf() {
  chown root:root $1/misdn-init.conf
  chmod 644 $1/misdn-init.conf
  mv $1/misdn-init.conf /etc
}

zaptelConf() {
  chown root:root $1/zaptel.conf
  chmod 644 $1/zaptel.conf
  mv $1/zaptel.conf /etc
  # Add driver load order if config file exist
  if [ -f "$1/zaptel" ] ; then
    chown root:root $1/zaptel
    chmod 644 $1/zaptel
    mv $1/zaptel $ZAPTEL_KMOD
  fi
}

# return asterisk status 
asterisk_status () {
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else 
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    fi
    else echo "OFF"
  fi  
}

# start asterisk 
asterisk_start () {
  if [ ! -e "$AST_RUN_DIR/asterisk.pid" ]; then
    $AST_ASTERISK $AST_ASTERISK_START >/dev/null 2>/dev/null
    sleep 3
  fi
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null 
      echo "OFF"
    fi
    else echo "OFF"
  fi 
}

# stop asterisk 
asterisk_stop () {
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then
    $AST_ASTERISK $AST_ASTERISK_STOP >/dev/null 2>/dev/null
    sleep 3
  fi
  if [ -e "$AST_RUN_DIR/asterisk.pid" ]; then 
    if [ -d "/proc/`cat $AST_RUN_DIR/asterisk.pid`" ]; then
      echo "ON"
    elif [ $(ps ax | grep safe_asterisk | grep -v grep | wc -l) -ne "0" ]; then
      kill -9 `ps ax |grep safe_asterisk |grep -v grep|awk '{print $1;}'` >/dev/null 2>/dev/null
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null
      echo "OFF"
    else
      rm $AST_RUN_DIR/asterisk.* >/dev/null 2>/dev/null 
      echo "OFF"
    fi
    else echo "OFF"
  fi  
}

zaptelInit() {
  $ZAPTEL_INIT $1
  # If zaptel stop fails, most likely there is 
  # a module using zaptel that was not listed.
  RETVAL=$?
  if [ "$RETVAL" != "0" ] && [ "$1" == "stop" ] ; then
    unloadZaptelModules
  fi
}

misdnInit() {
  $MISDN_INIT $1
}

astGenKey() {
  $AST_GENKEY $1
}

shutdown() {
  $SYS_SHUTDOWN $1 now
}

# unload remaining zaptel modules
unloadZaptelModules()
{
  COUNT=1
  for ITEM in `lsmod | grep ^zaptel`
  do
    # Abort if we don't have module zaptel
    if [ "$ITEM" != "zaptel" ] && [ $COUNT -eq 1 ] ; then
      return;
    fi

    # Skip the first 3 entries Module, Size, Used by
    if [ $COUNT -le 3 ] ; then
      COUNT=`expr $COUNT + 1`
      continue;
    fi 
    
    # Get the module list using zaptel
    MODULES=`echo $ITEM | sed y/,/\ /`

    # Try to unload one by one
    for MODULE in $MODULES 
    do
      rmmod $MODULE > /dev/null 2> /dev/null
    done

    # Now lets try to stop service zaptel again
    # This will unload zaptel, remove lock etc
    $ZAPTEL_INIT stop

  done
}

# print vars used in this script
dump_vars() {
  echo "Variables used in vo-tools.sh and vo.cfg:"
  echo "AST_CONF_DIR=$AST_CONF_DIR"
  echo "AST_SPOOL_DIR=$AST_SPOOL_DIR"
  echo "AST_SOUND_DIR==$AST_SOUND_DIR"
  echo "AST_AGI_DIR=$AST_AGI_DIR"
  echo "AST_KEY_DIR=$AST_KEY_DIR"
  echo "AST_GENKEY=$AST_GENKEY"
  echo "MISDN_INIT=$MISDN_INIT"
  echo "ZAPTEL_INIT=$ZAPTEL_INIT"
  echo "DISTRO=$DISTRO"
  echo "ZAPTEL_KMOD=$ZAPTEL_KMOD"
  echo "SYS_SHUTDOWN=$SYS_SHUTDOWN"
  echo "VOICEONE_RELDIR=$VOICEONE_RELDIR"
  echo "VOICEONE_WEBSERVICES_RELDIR=$VOICEONE_WEBSERVICES_RELDIR"  
}


# Only for debugging
debug() {
  echo "$(date +"%Y%m%d-%H:%M"): Debug of vo-tools.sh" >> $DEBUG_VOT
  echo "=>Function:$1" >> $DEBUG_VOT
  echo "=>Param1  :$2" >> $DEBUG_VOT
  echo "=>Param2  :$3" >> $DEBUG_VOT
  echo "=>Param3  :$4" >> $DEBUG_VOT
}

# Debugging enabled ? (Needs to be enabled in vo.cfg)
if [ "$DEBUG_VOT" != "" ]; then debug $1 $2 $3 $4; fi

#
### Evaluate CMD and execute ###
#
case "$1" in
  
  install)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    install $2
	exit 0;;
  
  inkey)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    inKey $2
	exit 0;;
  
  zaptel_conf)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    zaptelConf $2
	exit 0;;

  misdn_conf)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    misdnConf $2
	exit 0;;
  
  zaptel_init)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    zaptelInit $2
	exit 0;;

  misdn_init)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    misdnInit $2
	exit 0;;

  astgenkey)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    astGenKey $2
	exit 0;;
  
  deleteAudiofile) 
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    deleteAudiofile $2
	exit 0;;
	
  renameAudiofile) 
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    renameAudiofile $2 $3
	exit 0;;
	
  shutdown) 
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    shutdown $2 $3
	exit 0;;
	
  voicemail)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    voicemail $2 $3
	exit 0;;
  
  transferVoiceMailMessage)
    if [ "$#" -ne "5" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    transferVoiceMailMessage $2 $3 $4 $5
	exit 0;;
  
  messageChangeFolder)
    if [ "$#" -ne "6" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    messageChangeFolder $2 $3 $4 $5 $6
	exit 0;;
  
  deleteVoicemailMessage)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    deleteVoicemailMessage $2 $3
	exit 0;;
  
  getVoiceMailAnswers)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    getVoiceMailAnswers $2 $3
	exit 0;;
  
  transferVoiceMailAnswer)
  if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    transferVoiceMailAnswer $2 $3
	exit 0;;
  
  setVoiceMailAnswer)
    if [ "$#" -ne "4" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    setVoiceMailAnswer $2 $3 $4
	exit 0;;
  
  deleteVoiceMailAnswer)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    deleteVoiceMailAnswer $2 $3
	exit 0;;
  
  getMohDir)
    if [ "$#" -ne "1" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    getMohDir
	exit 0;;
  
  mohMakeSongsList)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    mohMakeSongsList $2 $3
	exit 0;;
  
  mohSetSong)
    if [ "$#" -ne "4" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    mohSetSong "$2" "$3" "$4"
	exit 0;;
  
  mohDelFolder)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    mohDelFolder "$2"
	exit 0;;
  
  mohMoveSong)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    mohMoveSong "$2" "$3"
	exit 0;;
  
  mohDelSong)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    mohDelSong "$2"
	exit 0;;
  
  getSoundsDir)
    if [ "$#" -ne "1" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    getSoundsDir
	exit 0;;
  
  soundsInit)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsInit $2
	exit 0;;
  
  soundsCopy)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsCopy $2 $3
	exit 0;;
  
  soundsMoveToSandbox)
    if [ "$#" -ne "4" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsMoveToSandbox $2 $3 $4
	exit 0;;
  
  soundsClear)
    if [ "$#" -ne "2" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsClear $2
	exit 0;;
  
  soundsMoveToSounds)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsMoveToSounds $2 $3
	exit 0;;
  
  soundsCreateEmptyFile)
    if [ "$#" -ne "3" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    soundsCreateEmptyFile $2 $3 
	exit 0;;
  
  asterisk_status)
    if [ "$#" -ne "1" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    asterisk_status
	exit 0;;
  
  asterisk_start)
    if [ "$#" -ne "1" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    asterisk_start
	exit 0;;
  
  asterisk_stop)
    if [ "$#" -ne "1" ]; then echo "Wrong number of parameter:$#"; exit 1; fi
    asterisk_stop
	exit 0;;
  
  dump_vars)
    dump_vars
  exit 0;;
    
  *)
    echo "Error: Unknown Command:$0" 
	echo "Supported CMDs:" 
	echo "deleteAudiofile filename"
	echo "renameAudiofile filename newFilename"
	echo "install WebServerDocumentRoot"
	echo "inkey FullPathFilename"
	echo "zaptel_conf FullPathSandbox"
	echo "misdn_conf FullPathSandbox"
  echo "zaptel_init cmd"
	echo "misdn_init cmd"
	echo "astgenkey filename"
	echo "shutdown action"
  echo "voicemail extension toPath"
  echo "transferVoiceMailMessage extension topath file"
  echo "messageChangeFolder extension tofile togroup file group"
  echo "deleteVoicemailMessage extension file"
  echo "getVoiceMailAnswers extension topath"
  echo "transferVoiceMailAnswer extension file"
  echo "setVoiceMailAnswer extension file type"
  echo "deleteVoiceMailAnswer extension file"
  echo "getMohDir"
  echo "mohMakeSongsList class sandboxpath"
  echo "mohSetSong folder song linkname"
  echo "mohDelFolder folder"
  echo "mohMoveSong frompath toname"
  echo "mohDelSong filename"
  echo "getSoundsDir"
  echo "soundsInit topath"
  echo "soundsCopy frompath topath"
  echo "soundsMoveToSandbox frompath topath filemoved"
  echo "soundsClear path"
  echo "soundsMoveToSounds frompath filename"
  echo "soundsCreateEmptyFile folderpath filename"
  echo "asterisk_status"
  echo "asterisk_start"
  echo "asterisk_stop" 
  echo "dump_vars"
	exit 1;;
esac
